const CommentModal = require("@/model/Comment");
const comment = {
  async GetCount(data) {
    let count = 0;
    let query = `
    SELECT COUNT(1) AS count
    FROM comment c
    LEFT JOIN user_info ui ON c.user_id = ui.id
    WHERE 1=1
  `;
    const params = [];

    if (data.type !== undefined && +data.type !== 0) {
      query += " AND c.type = ?";
      params.push(data.type);
    }

    if (data.is_review !== undefined && +data.is_review !== null) {
      query += " AND c.is_review = ?";
      params.push(data.is_review);
    }

    if (data.nickname) {
      query += " AND ui.nickname LIKE ?";
      params.push(`%${data.nickname}%`);
    }
    const [rows] = await global.mysqlPool.execute(query, params);
    if (rows.length > 0) {
      count = rows[0].count;
    }
    return count;
  },
  async GetList(data) {
    let query = `
    SELECT c.id, u1.avatar, u1.nickname, u2.nickname AS reply_nickname, a.title AS article_title,
           c.content, c.type, c.created_at, c.is_review
    FROM comment c
    LEFT JOIN article a ON c.topic_id = a.id
    LEFT JOIN user_info u1 ON c.user_id = u1.id
    LEFT JOIN user_info u2 ON c.reply_user_id = u2.id
    WHERE 1=1
  `;
    let params = [];

    if (data.type !== undefined && +data.type !== 0) {
      query += " AND c.type = ?";
      params.push(data.type);
    }

    if (data.is_review !== undefined && data.is_review !== null) {
      query += " AND c.is_review = ?";
      params.push(data.is_review);
    }

    if (data.nickname !== undefined && data.nickname !== "") {
      query += " AND u1.nickname LIKE ?";
      params.push(`%${data.nickname}%`);
    }

    query += " ORDER BY c.id DESC LIMIT ? OFFSET ?";

    params.push(data.page_size, data.page_size * (data.page_num - 1));

    const [rows] = await global.mysqlPool.execute(query, params);
    return rows;
  },
  async GetArticleCommentCount(articleId) {
    const [rows] = await global.mysqlPool.execute(
      `SELECT COUNT(*) AS count FROM comment
       WHERE topic_id = ? AND type = 1 AND is_review = 1`,
      [articleId]
    );
    const count = rows[0].count;
    return count;
  },
  async GetFrontList({ type, page_size, page_num, topic_id }) {
    let query = `
    SELECT u.nickname, u.avatar, u.website, c.id, c.user_id, c.content, c.created_at
    FROM comment c
    LEFT JOIN user_info u ON c.user_id = u.id
    WHERE type = ? AND c.is_review = 1 AND parent_id = 0
    `;
    let params = [type];

    if (topic_id && topic_id !== 0) {
      query += " AND topic_id = ?";
      params.push(topic_id);
    }
    query += " ORDER BY c.id DESC";
    // query += " LIMIT ? OFFSET ?";
    // params.push(page_size, (page_num - 1) * page_size);

    const [rows] = await global.mysqlPool.execute(query, params);
    list = rows;

    const [totalRows] = await global.mysqlPool.execute(
      "SELECT COUNT(*) AS total FROM comment WHERE type = ? AND is_review = 1 AND parent_id = 0",
      [type]
    );
    total = totalRows[0].total;

    return { commentList: rows, total: rows.length };
  },
  async GetReplyList(ids) {
    //没评论
    if (ids.length === 0) return [];
    // 将评论id列表转换为逗号分隔的字符串
    const idString = ids.join(",");

    //根据评论id列表获取回复列表
    const [rows] = await global.mysqlPool.execute(
      `
    SELECT c.user_id, u1.nickname, u1.avatar, u1.website, c.reply_user_id, u2.nickname AS reply_nickname,
        u2.website AS reply_website, c.id, c.parent_id, c.content, c.created_at
    FROM comment c
    JOIN user_info u1 ON c.user_id = u1.id
    JOIN user_info u2 ON c.reply_user_id = u2.id
    WHERE c.is_review = 1 AND c.parent_id IN (${idString})
    ORDER BY c.parent_id, c.created_at ASC
`
    );
    return rows;
  },
  async GetReplyCountListByCommentId(ids) {
    if (ids.length === 0) {
      return []; // 如果ids数组为空，直接返回空数组
    }
    const [rows] = await global.mysqlPool.execute(
      `
    SELECT parent_id AS comment_id, COUNT(1) AS reply_count
    FROM comment
    WHERE is_review = 1 AND parent_id IN (${ids.map(() => "?").join(", ")})
    GROUP BY parent_id
    `,
      [...ids]
    );
    return rows;
  },
  async GetReplyListByCommentId(id) {
    let query = `SELECT c.user_id, u1.nickname, u1.avatar, u1.website, c.reply_user_id, u2.nickname AS reply_nickname, 
        u2.website AS reply_website, c.id, c.parent_id, c.content, c.created_at
        FROM comment c
        JOIN user_info u1 ON c.user_id = u1.id
        JOIN user_info u2 ON c.reply_user_id = u2.id
        WHERE c.is_review = 1 AND parent_id = ?
        ORDER BY c.id ASC`;
    const [rows] = await global.mysqlPool.execute(query, [id]);
    return rows;
  },
};

module.exports = comment;
